﻿using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using static FastIOC.Enum.CotainerEnum;

namespace FastCloud.Base.DB
{
    [Component]
    public class DB_Attach
    {
        [AutoWired]
        private DB_AttachInfo DB_AttachInfo;

        public void InsertAttach(Frame_AttachInfo AttachInfo, byte[] AttachContent)
        {
            IAttachStorage AttachService = InjectContext.ResolveNamed<IAttachStorage>(AppConfigUtil.Configuration["Attach:AttachStorageType"]);
            AttachService.InsertAttach(AttachInfo, AttachContent);
            DB_AttachInfo.Insert(AttachInfo);
        }

        public void DeleteAttach(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = DB_AttachInfo.SelectOne(AttachGuid);
            if (AttachInfo!=null)
            {
                IAttachStorage AttachService = InjectContext.ResolveNamed<IAttachStorage>(AttachInfo.StorageType);
                AttachService.DeleteAttach(AttachGuid);
                DB_AttachInfo.DeleteByID(AttachGuid);
            }
        }

        public string GetAttachUrl(string AttachGuid)
        {
            Frame_AttachInfo AttachInfo = DB_AttachInfo.SelectOne(AttachGuid);
            IAttachStorage AttachService = InjectContext.ResolveNamed<IAttachStorage>(AttachInfo.StorageType);
            return AttachService.GetAttachUrl(AttachGuid);
        }

        public List<Frame_AttachInfo> SelectAttachByGroupGuid(string GroupGuid)
        {
            List<ParamEntity> ParamList = new List<ParamEntity>() { new ParamEntity() { ParamName = "GroupGuid", ParamValue = GroupGuid } };
            return DB_AttachInfo.SelectAllList("*", "GroupGuid=@GroupGuid", ParamList,"AddDate");
        }

        public List<Frame_AttachInfo> SelectAttachByGroupGuid(string GroupGuid,string GroupType)
        {
            List<ParamEntity> ParamList = new List<ParamEntity>() 
            { 
                new ParamEntity() { ParamName = "GroupGuid", ParamValue = GroupGuid },
                new ParamEntity() { ParamName = "GroupType", ParamValue = GroupType }
            };
            return DB_AttachInfo.SelectAllList("*", "GroupGuid=@GroupGuid and GroupType=@GroupType", ParamList, "AddDate");
        }
    }
}
